package com.eric.windows.imgDispose;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.app.Activity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.LinearLayout;


import com.eric.windows.R;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.Timer;
import java.util.TimerTask;

@EActivity(R.layout.activity_surface_test)
public class SurfaceWaveActivity extends Activity  {

    @ViewById(R.id.sv_surfaces)
    SurfaceView svSurfaces;
    @ViewById(R.id.ll_surface)
    LinearLayout linearLayout;

    private Paint paint;
    final  int HEIGHT = 820;
    final  int WIDTH = 820;
    final  int X_OFFSET = 5;
    private int cx = X_OFFSET;
    int centerY = HEIGHT / 2;
    Timer timer = new Timer();
    TimerTask  timerTask  = null;
    private SurfaceHolder holder;
    int cy;

    @AfterViews
    void initViews(){
        linearLayout.setVisibility(View.VISIBLE);
        paint = new Paint();
        holder = svSurfaces.getHolder();
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(5);

        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {

            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                drawBack(holder);
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                if (timer != null){
                    timer.cancel();
                }
            }
        });
    }

    @Click({R.id.btn_sin_surface,R.id.btn_cos_surface})
    public void onClick(final View view) {
        final int id = view.getId();
        drawBack(holder);
        cx = X_OFFSET ;
        if (timerTask != null){
            timerTask.cancel();
        }

        timerTask = new TimerTask() {
            @Override
            public void run() {
                switch (id) {
                    case R.id.btn_sin_surface:
                        cy = centerY - (int)(100 * Math.sin((cx - 5) * 2 * Math.PI / 150));
                        break;
                    case R.id.btn_cos_surface:
                        cy = centerY - (int)(100 * Math.cos((cx - 5) * 2 * Math.PI / 150));
                        break;
                }

                Canvas canvas = holder.lockCanvas(new Rect(cx,cy - 2 ,cx + 2 , cy + 2));
                canvas.drawPoint(cx,cy,paint);
                cx ++;
                if (cx > WIDTH){
                    timerTask.cancel();
                    timerTask = null;
                }
                holder.unlockCanvasAndPost(canvas);
            }
        };
        timer.schedule(timerTask,0,30);

    }

    private void drawBack (SurfaceHolder holder){
        Canvas canvas = holder.lockCanvas();
        canvas.drawColor(Color.WHITE);
        Paint p = new Paint();
        p.setColor(Color.BLACK);
        p.setStrokeWidth(5);

        canvas.drawLine(X_OFFSET,centerY,WIDTH,centerY,p);
        canvas.drawLine(X_OFFSET,40,X_OFFSET,HEIGHT,p);
        holder.unlockCanvasAndPost(canvas);
        holder.lockCanvas(new Rect(0,0,0,0));
        holder.unlockCanvasAndPost(canvas);
    }


}
